//
//  telepathydb.m
//  tcpoctest
//
//  Created by apple on 13-8-7.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "telepathydb.h"
#import "telepathyInfo.h"
#import "TelepathyDataSource.h"

@implementation telepathydb

-(void)creatTable
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:@"create table if not exists telepathy(tid text primary key,correctmail text,\
mail1 text,mail1guess int,\
mail2 text,mail2guess int,\
mail3 text,mail3guess int,\
mail4 text,mail4guess int,\
mail5 text,mail5guess int,\
mail6 text,mail6guess int,\
mail7 text,mail7guess int,\
mail8 text,mail8guess int,\
mail9 text,mail9guess int,\
state int,usermail text,guessnum int)"];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)initData
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"SELECT * FROM telepathy where usermail = '%@'"
                        ,[DataControl shareControl].personDS.uinfo.mail];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [[DataControl shareControl].TelepathyDS DelAllTeleDic];
    
    FMResultSet *s = [db executeQuery:strsql];
    
    while ([s next])
    {
        telepathyInfo *tinfo = [[telepathyInfo alloc] init];
        
        tinfo.strid = [s stringForColumn:@"tid"];
        tinfo.correctmail = [s stringForColumn:@"correctmail"];
        
        for (int i = 0; i < 9; ++i) {
            NSString *strmail = [NSString stringWithFormat:@"mail%d",i + 1];
            NSString *guessmail = [s stringForColumn:strmail];
            NSString *strmailguess = [NSString stringWithFormat:@"mail%dguess",i + 1];
            int iguess = [s intForColumn:strmailguess];
            
            [tinfo.mailarr addObject:guessmail];
            [tinfo.guessDic setObject:[NSNumber numberWithInt:iguess] forKey:guessmail];
        }
        
        tinfo.iStateType = [s intForColumn:@"state"];
        tinfo.iGuessNum = [s intForColumn:@"guessnum"];
        
        [[DataControl shareControl].TelepathyDS AddTelepDic:tinfo];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)insertTelepInfo:(telepathyInfo*)tinfo
{
    if (db == nil || tinfo == nil) {
        return;
    }
    
    if (tinfo.mailarr.count != 9 || tinfo.guessDic.count != 9) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    NSString *strsql = [NSString stringWithFormat:@"SELECT * FROM telepathy \
where tid = '%@'"
                        ,tinfo.strid];
    
    [db open];
    
    FMResultSet *s = [db executeQuery:strsql];
    
    if (![s next]) {
        strsql = [NSString stringWithFormat:@"insert into telepathy \
(tid,correctmail,\
mail1,mail1guess,\
mail2,mail2guess,\
mail3,mail3guess,\
mail4,mail4guess,\
mail5,mail5guess,\
mail6,mail6guess,\
mail7,mail7guess,\
mail8,mail8guess,\
mail9,mail9guess,\
state,usermail,guessnum) \
values('%@','%@',\
'%@',%d,\
'%@',%d,\
'%@',%d,\
'%@',%d,\
'%@',%d,\
'%@',%d,\
'%@',%d,\
'%@',%d,\
'%@',%d,\
%d,'%@',%d \
)",
tinfo.strid,tinfo.correctmail,
tinfo.mailarr[0],[[tinfo.guessDic objectForKey:tinfo.mailarr[0]] intValue],
tinfo.mailarr[1],[[tinfo.guessDic objectForKey:tinfo.mailarr[1]] intValue],
tinfo.mailarr[2],[[tinfo.guessDic objectForKey:tinfo.mailarr[2]] intValue],
tinfo.mailarr[3],[[tinfo.guessDic objectForKey:tinfo.mailarr[3]] intValue],
tinfo.mailarr[4],[[tinfo.guessDic objectForKey:tinfo.mailarr[4]] intValue],
tinfo.mailarr[5],[[tinfo.guessDic objectForKey:tinfo.mailarr[5]] intValue],
tinfo.mailarr[6],[[tinfo.guessDic objectForKey:tinfo.mailarr[6]] intValue],
tinfo.mailarr[7],[[tinfo.guessDic objectForKey:tinfo.mailarr[7]] intValue],
tinfo.mailarr[8],[[tinfo.guessDic objectForKey:tinfo.mailarr[8]] intValue],
tinfo.iStateType,[DataControl shareControl].personDS.uinfo.mail,tinfo.iGuessNum];
        
        NSLog(@"%@",strsql);
    }
    else
    {
        strsql = [NSString stringWithFormat:@"update telepathy set \
correctmail = '%@',\
mail1 = '%@',mail1guess = %d,\
mail2 = '%@',mail1guess = %d,\
mail3 = '%@',mail1guess = %d,\
mail4 = '%@',mail1guess = %d,\
mail5 = '%@',mail1guess = %d,\
mail6 = '%@',mail1guess = %d,\
mail7 = '%@',mail1guess = %d,\
mail8 = '%@',mail1guess = %d,\
mail9 = '%@',mail1guess = %d,\
state = %d,usermail = '%@',guessnum = %d \
where tid = '%@'"
,tinfo.correctmail
,tinfo.mailarr[0],[[tinfo.guessDic objectForKey:tinfo.mailarr[0]] intValue]
,tinfo.mailarr[1],[[tinfo.guessDic objectForKey:tinfo.mailarr[1]] intValue]
,tinfo.mailarr[2],[[tinfo.guessDic objectForKey:tinfo.mailarr[2]] intValue]
,tinfo.mailarr[3],[[tinfo.guessDic objectForKey:tinfo.mailarr[3]] intValue]
,tinfo.mailarr[4],[[tinfo.guessDic objectForKey:tinfo.mailarr[4]] intValue]
,tinfo.mailarr[5],[[tinfo.guessDic objectForKey:tinfo.mailarr[5]] intValue]
,tinfo.mailarr[6],[[tinfo.guessDic objectForKey:tinfo.mailarr[6]] intValue]
,tinfo.mailarr[7],[[tinfo.guessDic objectForKey:tinfo.mailarr[7]] intValue]
,tinfo.mailarr[8],[[tinfo.guessDic objectForKey:tinfo.mailarr[8]] intValue]
,tinfo.iStateType,[DataControl shareControl].personDS.uinfo.mail,tinfo.iGuessNum
,tinfo.strid];
    }
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)UpdateStateType:(telepathyInfo*)tinfo
{
    if (db == nil || tinfo == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"update telepathy set \
state = %d where tid = '%@'"
                        ,tinfo.iStateType
                        ,tinfo.strid];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)DelTelepByID:(NSString *)strid
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"DELETE FROM telepathy WHERE tid='%@'"
                        ,strid];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

@end
